class Solution
{
public:
    bool checkValidGrid(vector<vector<int>> &grid)
    {
        if (grid[0][0] != 0) // 这个条件太坑了
        {
            return false;
        }
        int n = grid.size();
        unordered_map<int, pair<int, int>> m;
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                m[grid[i][j]] = {i, j};
            }
        }
        for (int step = 1; step < n * n; ++step)
        {
            auto [lastX, lastY] = m[step - 1];
            auto [x, y] = m[step];
            if ((abs(x - lastX) == 1 && abs(y - lastY) == 2) || (abs(x - lastX) == 2 && abs(y - lastY) == 1))
            {
                continue;
            }
            else
            {
                return false;
            }
        }
        return true;
    }
};